package com.example.javabasic.thread.classone.demo3;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @description 测试join()
 * @Author Lin FuYuan
 * @date 2021/3/29
 */
public class ThreadJoin<T> {


    public static void main(String[] args) throws InterruptedException {
        System.out.println("启动线程" + Thread.currentThread().getName());
        test1();
        Thread.sleep(1000);
        System.out.println(Thread.currentThread().getName() + "执行结束");
    }

    public static void test1() {
        Thread thread = Thread.currentThread();
        new Thread(() -> {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "运行结束");
        }, "子线程").start();
    }


    public List<T> completionService(List<Callable<T>> callableList, Executor executor) throws InterruptedException, ExecutionException {
        CompletionService<T> comp = new ExecutorCompletionService(executor);
        for (Callable<T> tCallable : callableList) {
            comp.submit(tCallable);
        }
        List<T> list = new ArrayList<>();
        for (int i = 0; i < callableList.size(); i++) {
            Future<T> take = comp.take();
            list.add(take.get());
        }
        return list;
    }

}
